﻿using System;
using System.Windows;
using Microsoft.VisualStudio.PlatformUI;

namespace SPTotalAutomationToolkit.SP2013IntranetProjectTemplate.Wizard
{
    public partial class WizardWindow : DialogWindow
    {
        private Page1 firstPage;
        private Page2a secondPageA;
        private Page2b secondPageB;
        private Page3 thirdPage;
        private const string firstPageLabel = "General portal and project information";
        private const string secondPageLabel = "Development Environment Settings";
        private const string thirdPageLabel = "Test Environment Settings";
        private const string DevEnvDomain = "contoso.local";

        internal WizardWindow(ProjectWizardModel presentationModel)
        {
            InitializeComponent();
            this.PresentationModel = presentationModel;
            firstPage = new Page1(this);
            secondPageA = new Page2a(this);
            secondPageA.Visibility = Visibility.Hidden;
            secondPageB = new Page2b(this);
            secondPageB.Visibility = Visibility.Hidden;
            thirdPage = new Page3(this);
            thirdPage.Visibility = Visibility.Hidden;
            nextButton.IsEnabled = false;
            previousButton.IsEnabled = false;
            finishButton.IsEnabled = false;
        }

        internal ProjectWizardModel PresentationModel { get; set; }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            headingLabel.Content = firstPageLabel;
            pageGrid.Children.Add(firstPage);
            pageGrid.Children.Add(secondPageA);
            pageGrid.Children.Add(secondPageB);
            pageGrid.Children.Add(thirdPage);
        }

        private void nextButton_Click(object sender, RoutedEventArgs e)
        {
            switch (PresentationModel.CurrentPage)
            {
                // Moving from Page 1 to Page 2
                case 1:
                    {
                        firstPage.Visibility = Visibility.Hidden;
                        if (PresentationModel.UseHostNamedSiteCollections)
                        {
                            secondPageB.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            secondPageA.Visibility = Visibility.Visible;
                        }
                        previousButton.IsEnabled = true;
                        nextButton.IsEnabled = true;
                        nextButton.IsDefault = true;
                        finishButton.IsEnabled = false;
                        headingLabel.Content = secondPageLabel;

                        if (PresentationModel.UseHostNamedSiteCollections)
                        {
                            // Set the suggested intranet host header based on the updated client name
                            if (secondPageB.hnscIntranetHostHeaderForDevTextBox.Text == string.Empty)
                            {
                                secondPageB.hnscIntranetHostHeaderForDevTextBox.Text = GenerateSuggestedIntranetWebAppHostHeaderForDev();
                            }

                            // Set the suggested people host header based on the updated client name
                            if (secondPageB.hnscPeopleHostHeaderForDevTextBox.Text == string.Empty)
                            {
                                secondPageB.hnscPeopleHostHeaderForDevTextBox.Text = GenerateSuggestedPeopleWebAppHostHeaderForDev();
                            }

                            // Set the suggested people web app port
                            if (secondPageB.hnscPeoplePortForDevTextBox.Text == string.Empty)
                            {
                                secondPageB.hnscPeoplePortForDevTextBox.Text = "8060";
                            }

                            // Set the suggested server name
                            if (secondPageB.hnscServerNameForDevTextBox.Text == string.Empty)
                            {
                                secondPageB.hnscServerNameForDevTextBox.Text = System.Net.Dns.GetHostEntry(Environment.MachineName).HostName;
                            }

                            // Set the suggested server web app port
                            if (secondPageB.hnscServerPortForDevTextBox.Text == string.Empty)
                            {
                                secondPageB.hnscServerPortForDevTextBox.Text = "80";
                            }

                        }
                        else
                        {
                            // Set the suggested intranet host header based on the updated client name
                            if (secondPageA.pbscIntranetHostHeaderForDevTextBox.Text == string.Empty)
                            {
                                secondPageA.pbscIntranetHostHeaderForDevTextBox.Text = GenerateSuggestedIntranetWebAppHostHeaderForDev();
                            }

                            // Set the suggested intranet web app port
                            if (secondPageA.pbscIntranetPortForDevTextBox.Text == string.Empty)
                            {
                                secondPageA.pbscIntranetPortForDevTextBox.Text = "80";
                            }

                            // Set the suggested people host header based on the updated client name
                            if (secondPageA.pbscPeopleHostHeaderForDevTextBox.Text == string.Empty)
                            {
                                secondPageA.pbscPeopleHostHeaderForDevTextBox.Text = GenerateSuggestedPeopleWebAppHostHeaderForDev();
                            }

                            // Set the suggested people web app port
                            if (secondPageA.pbscPeoplePortForDevTextBox.Text == string.Empty)
                            {
                                secondPageA.pbscPeoplePortForDevTextBox.Text = "80";
                            }

                        }
                    }
                    break;

                // Moving from Page 2 to Page 3
                case 2:
                    {
                        if (PresentationModel.UseHostNamedSiteCollections)
                        {
                            secondPageB.Visibility = Visibility.Hidden;
                        }
                        else
                        {
                            secondPageA.Visibility = Visibility.Hidden;
                        }                        
                        thirdPage.Visibility = Visibility.Visible;
                        nextButton.IsEnabled = false;
                        finishButton.IsEnabled = true;
                        finishButton.IsDefault = true;
                        headingLabel.Content = thirdPageLabel;
                    }
                    break;
            }
            
            PresentationModel.CurrentPage++;
        }

        // Display the first wizard page again and update related controls.
        private void previousButton_Click(object sender, RoutedEventArgs e)
        {
            switch (PresentationModel.CurrentPage)
            {
                case 3:
                    {
                        thirdPage.Visibility = Visibility.Hidden;
                        if (PresentationModel.UseHostNamedSiteCollections)
                        {
                            secondPageB.Visibility = Visibility.Visible;
                        }
                        else
                        {
                            secondPageA.Visibility = Visibility.Visible;
                        }                        
                        nextButton.IsEnabled = true;
                        nextButton.IsDefault = true;
                        finishButton.IsEnabled = false;
                        headingLabel.Content = secondPageLabel;
                    }
                    break;

                case 2:
                    {
                        if (PresentationModel.UseHostNamedSiteCollections)
                        {
                            secondPageB.Visibility = Visibility.Hidden;
                        }
                        else
                        {
                            secondPageA.Visibility = Visibility.Hidden;
                        }
                        firstPage.Visibility = Visibility.Visible;
                        nextButton.IsEnabled = true;
                        nextButton.IsDefault = true;
                        previousButton.IsEnabled = false;
                        headingLabel.Content = firstPageLabel;
                    }
                    break;
            }

            PresentationModel.CurrentPage--;
        }

        private void finishButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            Close();
        }

        private string GenerateSuggestedIntranetWebAppHostHeaderForDev()
        {
            string hostNameForDev;

            if (PresentationModel.UseHostNamedSiteCollections)
            {
                hostNameForDev = PresentationModel.ProjectNameWithoutSpaces.ToLower() + "." + PresentationModel.ClientNameWithoutSpaces.ToLower() + ".local";
            }
            else
            {
                //hostNameForDev = PresentationModel.ClientNameWithoutSpaces.ToLower() + "." + DevEnvDomain;
                hostNameForDev = PresentationModel.ProjectNameWithoutSpaces.ToLower() + "." + PresentationModel.ClientNameWithoutSpaces.ToLower() + ".local";
            }

            return hostNameForDev;
        }

        private string GenerateSuggestedPeopleWebAppHostHeaderForDev()
        {
            string hostNameForDev;

            if (PresentationModel.UseHostNamedSiteCollections)
            {
                hostNameForDev = "my." + PresentationModel.ClientNameWithoutSpaces.ToLower() + ".local";
            }
            else
            {
                //hostNameForDev = "people2013." + DevEnvDomain;
                hostNameForDev = "my." + PresentationModel.ClientNameWithoutSpaces.ToLower() + ".local";
            }

            return hostNameForDev;
        }
    }
}